/*
 * 
 */

package com.memorytech.starchive;

import jp.co.memory_tech.starchive.R;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TabHost;

import com.memorytech.starchive.db.BarcodeReaderDBHelper;
import com.memorytech.starchive.interfaceclass.BarcodeReaderInfo;
import com.memorytech.starchive.interfaceclass.OnMainActivityListener;

// TODO: Auto-generated Javadoc
/**
 * The Class MainActivity.
 * 
 * @author Tam-LT
 */
public class MainActivity extends TabActivity implements OnMainActivityListener {

    /**
     * Called when the activity is first created.
     * 
     * @param savedInstanceState the saved instance state
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        BarcodeReaderDBHelper barcodeReaderDBHelper = BarcodeReaderDBHelper.getInstance(this,
                getPackageName());
        barcodeReaderDBHelper.checkAndCopyDatabase();
        TabHost tabHost = getTabHost(); // The activity TabHost
        TabHost.TabSpec spec; // Resusable TabSpec for each tab
        Intent intent; // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, SearchActivity.class);

        // Initialize a TabSpec for each tab and add it to the TabHost

        // Search tabs
        spec = tabHost.newTabSpec("search").setIndicator("Search", null)
                .setContent(intent);
        tabHost.addTab(spec);

        // Camera tabs
        intent = new Intent().setClass(this, BarcodeCaptureActivity.class);
        spec = tabHost.newTabSpec("camera").setIndicator("Camera", null)
                .setContent(intent);
        tabHost.addTab(spec);

        // History tabs
        intent = new Intent().setClass(this, HistoryActivity.class);
        spec = tabHost.newTabSpec("history").setIndicator("History", null)
                .setContent(intent);
        tabHost.addTab(spec);

        // Setting tabs
        intent = new Intent().setClass(this, SettingActivity.class);
        spec = tabHost.newTabSpec("setting").setIndicator("Setting", null)
                .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(BarcodeReaderInfo.SEARCH);
    }

    public void changeTab(int tab, Object data) {
        getTabHost().setCurrentTab(tab);
        Activity activity = this.getCurrentActivity();
        if (activity instanceof SearchActivity) {
            SearchActivity detailActivity = (SearchActivity) activity;
            detailActivity.search((String) data);
        }
    }

    @Override
    public void onShowHideTabWidget(boolean isShowTabWidget) {
        // TODO Auto-generated method stub
        if (isShowTabWidget) {
            findViewById(android.R.id.tabs).setVisibility(View.VISIBLE);

        } else {
            findViewById(android.R.id.tabs).setVisibility(View.GONE);

        }

    }

    public void onClearCache() {
        // TODO Auto-generated method stub
        getTabHost().setCurrentTab(BarcodeReaderInfo.HISTORY);
        Activity activity = this.getCurrentActivity();
        HistoryActivity historyActivity = (HistoryActivity) activity;
        historyActivity.onClearCache();
        getTabHost().setCurrentTab(BarcodeReaderInfo.SETTING);

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

}
